<?php declare(strict_types=1);


namespace App\Lib\Wechat;

use Swoft\Bean\Annotation\Mapping\Bean;

/**
 * Class WechatHelper
 * @Bean()
 * @package App\Helper
 */
class WechatHelper
{
    /**
     * @var appid
     */
    private $appid;

    /**
     * @var secret
     */
    private $secret;

    /**
     * @var js_code
     */
    private $js_code;

    /**
     * @var grant_type
     */
    private $grant_type = 'authorization_code';

    /**
     * 获取openid_url
     * 文档 https://developers.weixin.qq.com/miniprogram/dev/api-backend/open-api/login/auth.code2Session.html
     * 参数[
     *      grant_type=authorization_code
     *      appid=APPID
     *      secret=APPSECRET
     *      js_code=code
     * ]
     * @var string
     */
    private $openid_url = 'https://api.weixin.qq.com/sns/jscode2session';

    /**
     * 获取access_token
     * 文档 https://developers.weixin.qq.com/miniprogram/dev/api-backend/open-api/access-token/auth.getAccessToken.html
     * 参数[
     *      grant_type=client_credential
     *      appid=APPID
     *      secret=APPSECRET
     * ]
     * @var string
     */
    private $access_token_url = 'https://api.weixin.qq.com/cgi-bin/token';

    /**
     * 公众号获取用户信息
     * 参数[
     *      access_token=ACCESS_TOKEN 网页授权token
     *      openid=OPENID
     *      lang=zh_CN
     * ]
     * @var string
     */
    private $user_info_url = 'https://api.weixin.qq.com/cgi-bin/user/info';

    /**
     * 小程序获取用户信息
     * 参数[
     *      access_token=ACCESS_TOKEN 网页授权token
     *      openid=OPENID
     *      lang=zh_CN
     * ]
     * @var string
     */
    private $min_user_info_url = 'https://api.weixin.qq.com/sns/userinfo';

    /**
     * @return string
     */
    public function getAppid(): string
    {
        return $this->appid;
    }

    /**
     * @param string $appid
     * @return $this
     */
    public function setAppid(string $appid)
    {
        $this->appid = $appid;
        return $this;
    }

    /**
     * @return string
     */
    public function getSecret(): string
    {
        return $this->secret;
    }

    /**
     * @param string $secret
     * @return $this
     */
    public function setSecret(string $secret)
    {
        $this->secret = $secret;
        return $this;
    }

    /**
     * @return string
     */
    public function getJsCode(): string
    {
        return $this->js_code;
    }

    /**
     * @param string $js_code
     * @return $this
     */
    public function setJsCode(string $js_code)
    {
        $this->js_code = $js_code;
        return $this;
    }

    /**
     * @return string
     */
    public function getGrantType(): string
    {
        return $this->grant_type;
    }

    /**
     * @param string $grant_type
     * @return $this
     */
    public function setGrantType(string $grant_type)
    {
        $this->grant_type = $grant_type;
        return $this;
    }

    /**
     * 获取openid url
     * @return string
     */
    public function getOpenidUrl(): string
    {
        return $this->openid_url;
    }

    /**
     * 获取access_token url
     * @return string
     */
    public function getAccessTokenUrl(): string
    {
        return $this->access_token_url;
    }

    /**
     * 获取公众号信url
     * @return string
     */
    public function getUserInfoUrl(): string
    {
        return $this->user_info_url;
    }

    /**
     * 获取小程序用户信息url
     * @return string
     */
    public function getMinUserInfoUrl(): string
    {
        return $this->min_user_info_url;
    }

    /**
     * 登录通过code换取信息
     * openid    string    用户唯一标识
     * session_key    string    会话密钥
     * unionid    string    用户在开放平台的唯一标识符，在满足 UnionID 下发条件的情况下会返回，详见 UnionID 机制说明。
     * errcode    number    错误码
     * errmsg    string    错误信息
     * @return string
     */
    public function onLogin()
    {
        //开始登录操作
        $base_uri = $this->openid_url;
        $uri = '';
        //方法参数传递
        $data = [];
        $data['appid'] = $this->appid;
        $data['secret'] = $this->secret;
        $data['js_code'] = $this->js_code;
        $data['grant_type'] = $this->grant_type;
        $result = curl($base_uri, $uri, 'GET', $data);
        return $result;
    }

    /**
     * 请求获取access_token
     * @return string
     */
    public function getAccessToken()
    {
        //准备请求链接
        $base_uri = $this->access_token_url;
        $uri = '';
        //方法参数传递
        $data = [];
        $data['grant_type'] = 'client_credential';
        $data['appid'] = $this->appid;
        $data['secret'] = $this->secret;
        //请求获取access_toen接口
        $result = curl($base_uri, $uri, 'GET', $data);
        return $result;
    }

    /**
     * 公众号获取用户信息
     * @param array $params
     * @return string
     */
    public function getUserInfoByOfficialAccount(array $params)
    {
        //准备请求链接
        $base_uri = $this->user_info_url;
        $uri = '';
        //方法参数传递
        $data = [];
        $data['access_token'] = $params['access_token'];
        $data['openid'] = $params['openid'];
        $data['lang'] = $params['lang'];
        //请求获取用户信息通过小程序
        $result = curl($base_uri, $uri, 'GET', $data);

        return $result;
    }

    /**
     * 小程序获取用户信息
     * @param array $params
     * @return string
     */
    public function getUserInfoByMin(array $params)
    {
        //准备请求链接
        $base_uri = $this->min_user_info_url;
        $uri = '';
        //方法参数传递
        $data = [];
        $data['access_token'] = $params['access_token'];
        $data['openid'] = $params['openid'];
        $data['lang'] = $params['lang'] ?? 'zh_CN';
        vdump($data);
        //请求获取用户信息通过小程序
        $result = curl($base_uri, $uri, 'GET', $data);

        return $result;
    }
}
